package com.truckdispatching.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.truckdispatching.R;
import com.truckdispatching.util.TruckLengthUtil;

public class ChoseCarLengthActivity extends BaseActivity implements OnClickListener {

	private Button mBtnBack;
	private GridView mGridView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chose_carlength_activity);

		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setOnClickListener(this);

		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setAdapter(new CarLengthAdapter(TruckLengthUtil.getTruckLengthArrayList()));
	}

	private class CarLengthAdapter extends BaseAdapter implements OnClickListener {
		private ArrayList<String> lengthList = null;

		public CarLengthAdapter(ArrayList<String> lengthList) {
			this.lengthList = lengthList;
		}

		@Override
		public int getCount() {
			if (lengthList != null) {
				return lengthList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if (lengthList != null && position < lengthList.size()) {
				return lengthList.get(position);
			}
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new Button(ChoseCarLengthActivity.this);
				convertView.setBackgroundResource(R.drawable.btn_chose);
			}
			convertView.setOnClickListener(this);
			String length = lengthList.get(position);
			((Button) convertView).setText(length);
			convertView.setTag(length);
			return convertView;
		}

		@Override
		public void onClick(View v) {
			String length = v.getTag().toString();
			Intent data = new Intent();
			data.putExtra("result", length);
			setResult(100, data);
			finish();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		default:
			break;
		}
	}

}
